#!/usr/bin/perl
#mirrorman-lib.pl

do "../web-lib.pl";
&init_config();
%access = get_module_acl();

sub read_mirrortab{
    open(MT, $config{'mirrortab_file'});
    my @mtab=<MT>;
    close(MT);
    return @mtab;
}

sub list_mtab{
    my @mtab=&read_mirrortab();
    my $mlines=@mtab;
    print "<table width=100% class=mode2><tr>\n";
    print "<td  valign=top><div class=main><table  class=mode1 border=0 bordercolor=#33ccff cellspacing=0 cellpadding=1 width=100%>\n";
    print "<tr class=title1> <th align=left>$text{'modname'}</th> ",
          "<th align=left>$text{'modpath'}</th> ",
          "<th align=left>$text{'modstatus'}</th>",

          "<th align=left>$text{'modcomment'}</th> </tr>\n";
    foreach(@mtab){
           $mtab_line=$_;
           @mline=split(/:/, $mtab_line);
           $mline_name=$mline[0]; 
           $mline_path=$mline[1];
           $mline_status=$mline[2];
           $mline_comment=$mline[3];
           print "<tr class=mode1>\n";
           print "<td><input type=radio name=modline value=\"$mtab_line\">$mline_name</td>\n";
	   if(-e $mline_path){
           	print "<td nowarp>$mline_path</td>\n";
	   }
	   else{
		print "<td nowarp><strike>$mline_path</strike></td>\n";
	   }
           print "<td nowarp>$mline_status</td>\n";
           print "<td nowarp>$mline_comment</td></tr>\n";
    }
    print "</table></div></td>\n";

    print "<td valign=top width=160><table class=mode1 border=0 cellspacing=8 cellpadding=1 width=100%>\n";
    print "<tr><td align=center><input type=submit name=mstart value=\"$text{'modstart'}\" onclick='return on_mstart_click($mlines);' class=button></td></tr>\n";
	
    printf "<tr><td align=center><input type=submit name=mstop value=\"$text{'modstop'}\" onclick='return on_mstop_click($mlines);' class=button></td></tr>\n";
    printf "<tr><td align=center><input type=submit name=mcreate value=\"$text{'modcreate'}\" onclick='return on_mcreate_click();' class=button></td></tr>\n";
    print "<tr><td align=center><input type=submit name=mdelete value=\"$text{'moddelete'}\" onclick=\'return on_mdelete_click($mlines);\' class=button></td></tr>\n";

    print "</table></td></tr>";
    print "<tr><td colspan=2><i>$text{'hint'}</i></td></tr>";
    print "</table>\n";    
}

sub get_mod_line{
    my ($mod, @b) = @_;
    my $leng = @b;
    my $m;
    $start_line = 1;
    $end_line = 2;
    for($m=0; $m < $leng; $m++){

        if ($b[$m]=~ /\[$mod\]/){
           $start_line = $m;
           for($p=$m+1; $p < $leng; $p++){
              if ($b[$p]=~ /\[/){
                  $end_line = $p -1;
                  last;
              } 
           }
        last; 
        }
    }
    return($start_line,$end_line);
}

sub addplus{
    $_[0]="#".$_[0];
}

sub rmplus{
    $_[0]=~ tr/#//d;
}

sub get_end_line{
    my (@el) = @_;
    my $leng =@el;
    my $n;
    for($n=0; $n < $leng; $n++){
        if($el[$n]=~ /\[end\]/){
           $endline = $n;
           last;
        }
    }   
    return $endline; 
}

sub if_exist_mod{

}

sub check_usrvol{
   my $volmark=0;
   open(FH, "/etc/nas/voltab");
   my @vol=<FH>;
   close(FH);
   $line_num=@vol;
   if($line_num>1){
      if($vol[1] =~ /^volume/){

          $volmark=1;
   
      }
   }
   return $volmark;
}

sub check_modname{
    my $modname=$_[0];
    my $checkmod=1;
    if(($modname=~ /[^a-zA-Z0-9]/) || ($modname =~ /^\d+(.*)/)){
       $checkmod=0; 
    }
    return $checkmod;
}

sub check_modpath{

}

sub change_rsync{
    my ($modline, $cmd) = @_;
    my @line=split(/:/, $modline);
    my $modname = $line[0];
    open(FH, "/etc/rsyncd.conf");
    @aa=<FH>;
    close(FH);
    $length=@aa;
    my ($startline, $endline) = &get_mod_line($modname, @aa);
    if($cmd eq "modstart"){
       for($i=$startline; $i<=$endline; $i++){
           if($aa[$i]=~ /^#/){
              &rmplus($aa[$i]);     
           }
       } 
    }
    if($cmd eq "modstop"){
       for($i=$startline; $i<=$endline; $i++){
           if(!($aa[$i]=~ /^#/)){
              &addplus($aa[$i]);
           }
       }
    }
    open(FH, ">>/sysvol/etc/rsyncd.conf.tmp");
    for($i=0; $i<$length; $i++){
        print FH $aa[$i];    
    }
    close(FH);
    unlink("/sysvol/etc/rsyncd.conf");
    rename("/sysvol/etc/rsyncd.conf.tmp", "/sysvol/etc/rsyncd.conf");
}

sub change_mirrortab_status{
    my ($old_line, $cmd) = @_;
    my @line=split(/:/, $old_line);
    if($cmd eq "modstart"){
       $new_line=$line[0].":".$line[1].":"."R".":".$line[3];
    }
    if($cmd eq "modstop"){
       $new_line=$line[0].":".$line[1].":"."S".":".$line[3];  
    }

    open(FH, "/sysvol/etc/mirrortab");
    @mirrortab=<FH>;
    close(FH);

    open(FHB, ">>/sysvol/etc/mirrortab.tmp");
    foreach(@mirrortab){
       if($_ eq $old_line){
          print FHB $new_line;
       }else{
          print FHB $_;
       }
    }
    close(FHB);
    unlink("/sysvol/etc/mirrortab");
    rename("/sysvol/etc/mirrortab.tmp", "/sysvol/etc/mirrortab");
}

sub add_mirrortab{

    my ($mline, @mtab) = @_;
    my $leng = @mtab;
    my $j;
    open(FH, ">>/sysvol/etc/mirrortab_tmp");
    foreach(@mtab){
       print FH $_;
    }
    print FH $mline."\n";
    unlink("/sysvol/etc/mirrortab");
    rename("/sysvol/etc/mirrortab_tmp","/sysvol/etc/mirrortab"); 
}

sub add_rsyncd_conf{
    my ($modname, $modpath, $modcomment) = @_;
    open(FH, "/sysvol/etc/rsyncd.conf");
    @a=<FH>;
    close(FH);
    my $endline = &get_end_line(@a);
    open(FH,">>/sysvol/etc/rsyncd.conf.tmp");
    for($i=0; $i<$endline; $i++){
        print FH $a[$i];
    }
    print FH "[".$modname."]\n";
    print FH "comment = ".$modcomment."\n";
    print FH "path = ".$modpath."\n";
    print FH $a[$endline];
    close(FH);
    unlink("/sysvol/etc/rsyncd.conf");
    rename("/sysvol/etc/rsyncd.conf.tmp", "/sysvol/etc/rsyncd.conf"); 
}

sub rm_mirrortab{
    my ($mline, @mtab) = @_;
    my $leng = @mtab;
    my $j;
    open(FH, ">>/sysvol/etc/mirrortab_tmp");
    foreach(@mtab){
       if($_ ne $mline){
          print FH $_;
       } 
    }
    close(FH);
    unlink("/sysvol/etc/mirrortab");
    rename("/sysvol/etc/mirrortab_tmp","/sysvol/etc/mirrortab");     
}

sub rm_rsyncd_conf{
    my ($modname, @rsyncd) = @_;
    my $length=@rsyncd;
    my ($startline, $endline) = &get_mod_line($modname, @rsyncd);
    open(FH, ">>/sysvol/etc/rsyncd.conf.tmp");
    for($i=0; $i<$startline; $i++){
        print FH $rsyncd[$i];
    }
    for($i=$endline+1; $i<=$length; $i++){
        print FH $rsyncd[$i];
    }
    close(FH);
    unlink("/sysvol/etc/rsyncd.conf");
    rename("/sysvol/etc/rsyncd.conf.tmp", "/sysvol/etc/rsyncd.conf");
}
1;
 
